/**
 * Created by hanzhendong on 2016/12/1.
 */
'use strict';

import { Injectable }     from '@angular/core';
import { Http, Response } from "@angular/http";
import 'rxjs/add/operator/toPromise';
import { ServelUrl } from'../environments/ServelUrl'
import { Observable } from "rxjs";
import 'rxjs/Rx';

@Injectable()
export class ChatService {


    // don't know why Injection isn't working without @Inject:
    // http://stackoverflow.com/questions/34449486/angular-2-0-injected-http-service-is-undefined
    constructor(private http: Http) {
    }

    private uid(): string {
        return Math.random().toString(35).substr(2, 10);
    }

    getChatList(): Promise<any[]> {
        return this.http.get(ServelUrl.Url.ChatList)
            .toPromise()
            .then(response => response.json().data as any[])
    }
    getChatLists () {
        return this.http.get(ServelUrl.Url.ChatList) // ...using post request
            .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
            .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
    }

}
